package com.kun.oa.controller;

import com.github.pagehelper.PageInfo;
import com.kun.oa.entity.Account;
import com.kun.oa.entity.Permission;
import com.kun.oa.entity.Role;
import com.kun.oa.service.IAccountService;
import com.kun.oa.service.IPermissionService;
import com.kun.oa.service.IRolePermissionService;
import com.kun.oa.service.IRoleService;
import com.kun.oa.vo.RespStat;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname ManagerController
 * @Created by FangKun
 * @Date 2023/1/8 0008 下午 21:19
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Reference(version = "1.0.0")
    private IAccountService accountService;

    @Reference(version = "1.0.0")
    private IPermissionService permissionService;

    @Reference(version = "1.0.0")
    private IRoleService roleService;

    @Reference(version = "1.0.0")
    private IRolePermissionService rolePermissionService;

    /**
     * 管理员修改用户密码
     * @param password
     * @return
     */
    @RequestMapping("/modifyUserByAdmin")
    public String modifyUserByAdmin(@RequestParam("id") Integer id,String password){
        //根据id帮用户修改密码
        int res = accountService.modifyUserByAdmin(id,password);

        if (res == 1){
            return "/manager/modify";
        } else {
            return "errorPage";
        }
    }

    /**
     * 角色管理跳转权限界面
     * @param id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("rolePermission/{id}")
    public  String rolePermission(@PathVariable int id, Model model, HttpServletRequest request) {

        Role role = roleService.findById(id);
        List<Permission> pList =  permissionService.findAll();
        model.addAttribute("pList", pList);
        model.addAttribute("role", role);
        return "manager/rolePermission";
    }

    /**
     * 账号管理
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/accountList")
    public String accountList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model){

        PageInfo<Account> accountList = accountService.findByPage(pageNum,pageSize);
        model.addAttribute("page", accountList);

        return "/manager/accountList";
    }

    /**
     * 角色管理
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/roleList")
    public String roleList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model){

        PageInfo<Role> roleList = roleService.findByPage(pageNum,pageSize);
        model.addAttribute("page", roleList);

        return "/manager/roleList";
    }

    /**
     * 权限管理
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/permissionList")
    public String permissionList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model){

        PageInfo<Permission> permissionList = permissionService.findByPage(pageNum,pageSize);
        model.addAttribute("page", permissionList);

        return "/manager/permissionList";
    }

    /**
     * 修改信息获取
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("permissionModify")
    public  String permissionModify(@RequestParam int id,Model model) {

        Permission permission = permissionService.findById(id);

        model.addAttribute("p", permission);
        return "manager/permissionModify";
    }

    /**
     * 权限添加页
     * @param model
     * @return
     */
    @RequestMapping("permissionAdd")
    public  String permissionAdd(Model model) {
        return "manager/permissionModify";
    }


    /**
     * 获取要修改的用户信息
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/modify")
    public String modifyUser(@RequestParam int id, HttpServletRequest request){
        System.out.println(id);
        Account account = accountService.selectById(id);
        request.getSession().setAttribute("current",account);
        return "/manager/modify";
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @RequestMapping("/deletePermissionById")
    @ResponseBody
    public RespStat deletePermissionById(@RequestParam int id){
        RespStat stat = new RespStat();

        boolean rolePermission = rolePermissionService.selectById(id);

        if (rolePermission){
            stat.setCode(000);
            stat.setMsg("删除失败!");
            return stat;
        }

        boolean res = permissionService.deletePermissionById(id);

        if (res){
            stat.setCode(200);
            stat.setMsg("删除成功!");
            return stat;
        }else{
            stat.setCode(001);
            stat.setMsg("删除失败!");
            return stat;
        }
    }


}
